import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

/**
 * @author 白sa能
 * @className
 * @date Created in 11:41 PM 4/27/21
 * @package PACKAGE_NAME
 * @Description
 */
public class JsoupPaShuJv {
    @Test
    public void test1() throws IOException {
        //从URL加载HTML
        Document document = Jsoup.connect("http://www.baidu.com").get();
        String title = document.title();
        //获取html中的标题
        System.out.println("title :"+title);

        //从字符串加载HTML
        String html = "<html><head><title>First parse</title></head>"
                + "<body><p>Parsed HTML into a doc.</p></body></html>";
        Document doc = Jsoup.parse(html);
        title = doc.title();
        System.out.println("title :"+title);

        //从文件加载HTML
        doc = Jsoup.parse(new File("F:\\jsoup\\html\\index.html"),"utf-8");
        title = doc.title();
        System.out.println("title :"+title);
    }

    public static void main(String[] args) throws IOException {
        String baseUrl = "https://new.qq.com/ch/antip/";
        for (int i = 0; i < 10; i++) {
            String url = String.format(baseUrl, i * 25);
            try {
                parsePage(url);
            } catch (Exception e) {
                System.out.println("Error !!");
            }
        }
    }

    public static void parsePage(String src) throws IOException {
        Document document = Jsoup.connect(src).get();
        Elements elements = document.select("div.item");
        for (Element element : elements) {
            String title = element.select("span.title").first().text();
            String url = element.select("div.hd > a").first().attr("href");
            System.out.println("title : " + title + " url : " + url);
        }
    }
}
